package com.redtrusssoftware.truckloaderpro;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class NewEventController extends Activity{


	
	/**
	 * @param args
	 */
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.event);
        
        GetEventResults();
        
		

                   	
    	
	}

	private void GetEventResults() {
		ParseQuery<ParseObject> query = ParseQuery.getQuery("Event");
        query.orderByAscending("startDate");
        query.setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK);
        query.findInBackground(new FindCallback<ParseObject>() {     	
        	@Override
			public void done(List<ParseObject> object, ParseException e) {
        		ArrayList<Event> tempArray = new ArrayList<Event>();
        		SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMM yyyy");
        		if (e == null) {
        	        // The query was successful.
        			for(ParseObject obj : object){	
        				Event event = new Event();
						String eventName = obj.getString("name");
						Date date = obj.getDate("startDate");
						event.setName(eventName);
						event.setDate(formatter.format(date));
						tempArray.add(event);
					//	Toast.makeText(getApplicationContext(), event.toString(),Toast.LENGTH_SHORT).show();					
					}					
        	    } else {
        	        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        	    }
        		ListView list = (ListView)findViewById(R.id.ListView01);            	
            	list.setAdapter(new CustomAdapter(NewEventController.this,tempArray));
            	list.setOnItemClickListener(new OnItemClickListener(){
        			@Override
        			public void onItemClick(AdapterView<?> a, View v, int position,long id) {
        				
        				LinearLayout parent = (LinearLayout) v;
        				TextView t = (TextView) parent.findViewById(R.id.name);
        				String nameText = t.getText().toString();
        				
        				Intent i = new Intent(getApplicationContext(),TruckController.class);
        				i.putExtra("test",nameText);
        				startActivity(i);
        				
        			}
        			
        		});
        	  }
        	
        	});        
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


}

	